package hrms.controller;

import hrms.entity.Post;
import hrms.entity.Recruitment;
import hrms.entity.Result;
import hrms.entity.WorkChange;
import hrms.service.PostService;
import javafx.geometry.Pos;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/post")
public class PostController {

    @Autowired
    PostService postService;

    @PostMapping("/getall")
    public Result selectAllPost(){
        List<Post> posts = postService.selectAllPost();
        Map<String, Object> map = new HashMap<>();
        map.put("postList", posts);
        return Result.success().addMap(map);
    }

    @PostMapping("/update")
    public Result updatePost(@RequestBody Post post){
        int res = postService.updatePost(post);
        Map<String,Object> map = new HashMap<String,Object>();
        map.put("res",res);
        return Result.success().addMap(map);
    }

    @PostMapping("/delete/{id}")
    public Result deletePost(@PathVariable("id") Integer id){
        int res = postService.deletePost(id);
        Map<String,Object> map = new HashMap<String,Object>();
        map.put("res",res);
        return Result.success().addMap(map);
    }

    @PostMapping("/add")
    public Result addPost(@RequestBody Post post){
        post.setCurNum(0);
        int res = postService.addPost(post);
        Map<String,Object> map = new HashMap<String,Object>();
        map.put("res",res);
        return Result.success().addMap(map);
    }

    @PostMapping("/search/{pname}")
    public Result searchById(@PathVariable("pname")String pname){
        Map<String,Object> map = new HashMap<String,Object>();
        List<Post> posts = postService.selectPostByName(pname);
        if (posts.size()==0){
            return Result.fail().error("请再次检查部门称谓");
        }
        map.put("postList",posts);
        return Result.success().addMap(map);
    }

    @GetMapping("/selectByDepartmentId")
    public Result selectByDepartmentId(Integer departmentId){

        List<Post> posts = postService.selectByDepartmentId(departmentId);

        Map<String, Object> map = new HashMap<>();
        map.put("postList", posts);

        return Result.success().addMap(map);
    }

    @GetMapping("/selectDepartmentNameById")
    public Result selectDepartmentNameById(Integer id){

        Post post = postService.selectDepartmentNameById(id);

        Map<String, Object> map = new HashMap<>();
        map.put("list", post);

        return Result.success().addMap(map);
    }
}
